package com.zlc.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.common.exception.AccountDisabledException;
import com.zlc.common.common.exception.BadCredentialsException;
import com.zlc.common.common.exception.BusinessException;
import com.zlc.common.common.service.AutowiredService;
import com.zlc.common.common.service.ContextHolder;
import com.zlc.common.core.resource.model.entity.MayanPlatSystemResource;
import com.zlc.common.core.resource.model.vo.MayanPlatSystemResourceVo;
import com.zlc.common.core.sys.service.MayanPlatParameterService;
import com.zlc.common.core.user.mapper.MayanPlatUserMapper;
import com.zlc.common.core.user.model.entity.MayanPlatUser;
import com.zlc.common.core.user.model.vo.MayanPlatLoginUserVo;
import com.zlc.common.core.user.model.vo.MayanPlatUserVo;
import com.zlc.common.model.resource.MyResourceVo;
import com.zlc.common.model.resource.ResourceEntity;
import com.zlc.common.model.role.DataRoleVo;
import com.zlc.common.model.role.UserRoleVo;
import com.zlc.common.model.user.UserInfo;
import com.zlc.common.model.user.UserPageRequest;
import com.zlc.common.util.*;
import com.zlc.platform.model.PermissionEntity;
import com.zlc.platform.service.DeptService;
import com.zlc.platform.service.ResourceService;
import com.zlc.platform.service.RoleService;
import com.zlc.platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

import static com.zlc.common.common.constant.PlatConstant.LoginConstant.*;
import static com.zlc.common.common.constant.PlatConstant.Status.OFF_JOB;
import static com.zlc.common.common.constant.PlatConstant.Status.ON_JOB;
import static com.zlc.common.common.constant.PlatformRedisConstant.*;
import static com.zlc.common.common.constant.SysParameterConstant.*;


@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends AutowiredService implements UserService {

    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private MayanPlatParameterService mayanPlatParameterService;
    @Autowired
    @Qualifier("TaskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private UserService userService;

    @Override
    public boolean needCheckCode() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String localAddr = IpUtils.getIp(request);
        String s = RedisUtils.get(MAYAN_USER_LOGIN_CHECK_CODE + localAddr);
        return !ObjectUtils.isEmpty(s);
    }

    private void addCheckCode() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String localAddr = IpUtils.getIp(request);
        RedisUtils.set(MAYAN_USER_LOGIN_CHECK_CODE + localAddr, 1);
    }

    private void removeCheckCode() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String localAddr = IpUtils.getIp(request);
        RedisUtils.delete(MAYAN_USER_LOGIN_CHECK_CODE + localAddr);
    }

    @Override
    public UserInfo login(String userAccount, String password) {
        AssertUtils.notNull(userAccount, "请输入账号");
        AssertUtils.notNull(password, "请输入密码");
        MayanPlatUserVo userVo = mayanPlatUserService.queryByUserAccount(userAccount);
        //校验用户
        checkLoginPassword(userVo, password);
        //设置token
        userVo.setPassword(null);
        UserInfo info = setToken(userVo);
        //登录日志
        saveLoginRecord(info);
        //是否需要修改密码
        setNeedChangePassFlag(info, userVo);
        //删除验证码缓存
        removeCheckCode();
        return info;
    }

    private void setNeedChangePassFlag(UserInfo info, MayanPlatUserVo userVo) {
        if (!ObjectUtils.isEmpty(userVo.getChangePassTime())) {
            Integer passTime = mayanPlatParameterService.getInteger(USER_PASSWORD_UPDATE_CYCLE);
            passTime = passTime == null ? 30 : passTime;
            if (DateUtils.getBetweenSecondNumber(userVo.getChangePassTime(), new Date()) >= passTime * 24 * 60 * 60) {
                info.setNeedChangePassFlag(true);
            }
        }
    }

    private void checkLoginPassword(MayanPlatUserVo userVo, String password) {
        if (userVo == null) {
            throw new BadCredentialsException("账号或密码错误", null);
        }
        if (OFF_JOB.equals(userVo.getStatus())) {
            throw new AccountDisabledException("账号已禁用", null);
        }
        //自动解锁时间
        Integer unlockTime = mayanPlatParameterService.getInteger(USER_PASSWORD_UNLOCK_INTERVAL);
        unlockTime = unlockTime == null ? 30 : unlockTime;
        Boolean lockFlag = userVo.getLockFlag();
        Date lockTime = userVo.getLockTime();
        if (Boolean.TRUE.equals(lockFlag)) {
            if (!ObjectUtils.isEmpty(lockTime) && DateUtils.getBetweenSecondNumber(lockTime, new Date()) >= unlockTime * 60) {
                userVo.setLockFlag(false);
                userVo.setErrorCount(0);
            } else {
                throw new BadCredentialsException("账号已锁定,请在" + unlockTime + "分钟后重试", null);
            }
        }
        if (!Md5Utils.MD5Encoder(password).equals(userVo.getPassword())) {
            Integer maxError = mayanPlatParameterService.getInteger(LOGIN_FAIL_MAX_TOTAL);
            maxError = maxError == null ? 5 : maxError;
            Integer nowErrorCount = getOrPutLoginErrorCount(userVo, maxError, unlockTime);
            int latest = maxError - nowErrorCount;
            BadCredentialsException badCredentialsException = new BadCredentialsException("账号或密码错误,您还有" + latest + "次机会!", null);
            //是否需要验证码
            if (nowErrorCount > 2) {
                Boolean needCheckCode = mayanPlatParameterService.getBoolean(LOGIN_FAIL_CHECK_CODE_FLAG);
                badCredentialsException.setNeedCheckCodeFlag(needCheckCode);
                //添加验证码缓存
                addCheckCode();
            }
            throw badCredentialsException;
        }
        userVo.setLockFlag(false);
        userVo.setErrorCount(0);
        userService.asyncSaveUser(userVo);
    }

    /**
     * 异步保存用户
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void asyncSaveUser(MayanPlatUserVo userVo) {
        ContextHolder.setDefaultUser();
        mayanPlatUserService.saveVo(userVo);
    }

    private Integer getOrPutLoginErrorCount(MayanPlatUserVo userVo, Integer maxErrorCount, Integer unlockTime) {
        Integer errorCount = userVo.getErrorCount();
        if (!ObjectUtils.isEmpty(errorCount)) {
            errorCount = errorCount + 1;
        } else {
            errorCount = 1;
        }
        userVo.setErrorCount(errorCount);
        if (errorCount >= maxErrorCount) {
            userVo.setLockFlag(true);
            userVo.setLockTime(new Date());
            userService.asyncSaveUser(userVo);
            throw new BadCredentialsException("账号已锁定,请在" + unlockTime + "分钟后重试", null);
        }
        userService.asyncSaveUser(userVo);
        return errorCount;
    }

    public static void main(String[] args) {
        String s = Md5Utils.MD5Encoder("123456");
        System.out.println();
    }

    @Override
    public void outLogin() {
        removeLogin(UserHelper.getNowUser());
    }

    private void saveLoginRecord(UserInfo userInfo) {
        MayanPlatLoginUserVo loginUserVo = new MayanPlatLoginUserVo();
        loginUserVo.setUserAccount(userInfo.getUserAccount());
        loginUserVo.setLoginPlat("WEB");
        loginUserVo.setToken(userInfo.getToken());

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String localAddr = IpUtils.getIp(request);
        Object systemCode = request.getAttribute("platform");
        loginUserVo.setIp(localAddr);
        loginUserVo.setLoginTime(new Date());
        loginUserVo.setTokenOutTime(DateUtils.getNextDataDay(new Date(), 7));
        loginUserVo.setStatus(LOGIN_STATUS_ON);
        loginUserVo.setSystemCode(systemCode == null ? "" : systemCode.toString());
        mayanPlatLoginUserService.saveVo(loginUserVo);
    }

    private UserInfo setToken(MayanPlatUserVo userVo) {
        UserInfo info = new UserInfo();
        info.setToken(PlatUtils.uuid());
        info.setUserAccount(userVo.getUserAccount());
        info.setLoginTime(new Date());
        //用户信息
        info.setUser(userVo);
        deptService.loadUserInfo(info);
        //用户全部角色信息
        List<UserRoleVo> roleVos = roleService.getRoleByAccount(userVo.getUserAccount());
        for (UserRoleVo roleVo : roleVos) {
            List<UserRoleVo> roles = info.getRoles();
            if (roles != null && roles.stream().noneMatch(r -> r.getRoleCode().equals(roleVo.getRoleCode()))) {
                roles.add(roleVo);
            }
        }
        //用户数据权限
        List<UserRoleVo> roles = info.getRoles();
        if (!ObjectUtils.isEmpty(roles)) {
            List<String> roleCodes = roles.stream().map(UserRoleVo::getRoleCode).collect(Collectors.toList());
            DataRoleVo dataRoleVo = roleService.queryDataRoleByAccount(roleCodes);
            info.setDataRole(dataRoleVo);
        }
        refreshToken(info);
        ContextHolder.set(NOW_USER, info);
        //token有效期
        Integer timeOut = mayanPlatParameterService.getInteger(USER_SESSION_MAX_INACTIVE);
        long timeOutValue = timeOut == null ? LOGIN_TIME_OUT_SECOND : timeOut * 60 * 60;
        RedisUtils.set(MAYAN_PLATFORM_LOGIN_USER + info.getToken(), info, timeOutValue);
        RedisUtils.setAdd(MAYAN_PLATFORM_LOGIN_USER_TOKEN + info.getUserAccount(), info.getToken());
        RedisUtils.setAdd(MAYAN_PLATFORM_LOGIN_CACHE, info.getToken());
        return info;
    }

    private void refreshToken(UserInfo userInfo) {
        //是否可多地登录
        Boolean canMoreLogin = mayanPlatParameterService.getBoolean(USER_LOGIN_MORE_PEOPLE);
        if (Boolean.TRUE.equals(canMoreLogin)) {
            //如果可多地登录,获取已有token
            return;
        }
        removeAllLogin(userInfo);
    }

    private void removeAllLogin(UserInfo info) {
        Set<Object> tokens = RedisUtils.setGet(MAYAN_PLATFORM_LOGIN_USER_TOKEN + info.getUserAccount());
        if (!ObjectUtils.isEmpty(tokens)) {
            for (Object token : tokens) {
                RedisUtils.delete(MAYAN_PLATFORM_LOGIN_USER + token);
                RedisUtils.delete(MAYAN_SYSTEM_MY_RESOURCE_CACHE + info.getUserAccount());
            }
        }
    }

    /**
     * 删除登录信息
     */
    private void removeLogin(UserInfo info) {
        Set<Object> tokens = RedisUtils.setGet(MAYAN_PLATFORM_LOGIN_USER_TOKEN + info.getUserAccount());
        if (!ObjectUtils.isEmpty(tokens)) {
            Object token = tokens.stream().filter(t -> String.valueOf(t).equals(info.getToken())).findFirst().orElse(null);
            if (!ObjectUtils.isEmpty(token)) {
                //登录日志修改
                MayanPlatLoginUserVo loginUserVo = mayanPlatLoginUserService.queryByToken(String.valueOf(token));
                if (!ObjectUtils.isEmpty(loginUserVo)) {
                    loginUserVo.setOutTime(new Date());
                    loginUserVo.setStatus(LOGIN_STATUS_OFF);
                    mayanPlatLoginUserService.saveVo(loginUserVo);
                }
                RedisUtils.delete(MAYAN_PLATFORM_LOGIN_USER + token);
                RedisUtils.delete(MAYAN_SYSTEM_MY_RESOURCE_CACHE + info.getUserAccount());
                RedisUtils.removeSet(MAYAN_PLATFORM_LOGIN_CACHE, Collections.singletonList(token));
            }
        }
    }

    @Override
    public UserInfo get() {
        return UserHelper.getNowUser();
    }

    @Override
    @SuppressWarnings("unchecked")
    public PermissionEntity getMenu(String systemCode) {
        PermissionEntity entity = new PermissionEntity();
        AssertUtils.isTrue(!ObjectUtils.isEmpty(systemCode), "缺少系统编码参数");
        //返回值
        List<MyResourceVo> resourceList = new ArrayList<>();
        UserInfo nowUser = UserHelper.getNowUser();
        String resourceJson = RedisUtils.get(MAYAN_SYSTEM_MY_RESOURCE_CACHE + nowUser.getUserAccount());
        if (!ObjectUtils.isEmpty(resourceJson)) {
            try {
                return JsonHelper.parseJson(resourceJson, PermissionEntity.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        List<UserRoleVo> roles = nowUser.getRoles();
        if (ObjectUtils.isEmpty(roles) && !"1".equals(nowUser.getUser().getAdminFlag())) {
            return entity;
        }
        List<String> roleCodes = roles.stream().map(UserRoleVo::getRoleCode).collect(Collectors.toList());
        //当前用户拥有的所有资源
        List<MayanPlatSystemResourceVo> systemResourceVos = new ArrayList<>();
        if("1".equals(nowUser.getUser().getAdminFlag())){
            LambdaQueryWrapper<MayanPlatSystemResource> resourceWrapper = new LambdaQueryWrapper<>();
            resourceWrapper.eq(MayanPlatSystemResource::getSystemCode, systemCode);
            List<MayanPlatSystemResource> mayanPlatSystemResources = mayanPlatSystemResourceMapper.selectList(resourceWrapper);
            systemResourceVos.addAll(BeanCopyUtils.copyObjList(mayanPlatSystemResources,MayanPlatSystemResourceVo.class));
        }else{
            systemResourceVos =
                    mayanPlatResourceRoleMapper.getResourceByRoleCodeAndSystemCode(roleCodes, systemCode);
        }
        loadParentResource(systemResourceVos);
        //递归 获取根菜单
        List<MayanPlatSystemResourceVo> resourceVos = systemResourceVos.stream()
                .filter(s -> ObjectUtils.isEmpty(s.getParentResourceId()))
                .sorted(Comparator.comparing(MayanPlatSystemResourceVo::getOrder)).collect(Collectors.toList());
        for (MayanPlatSystemResourceVo resourceVo : resourceVos) {
            MyResourceVo myResourceVo = BeanCopyUtils.copyObj(resourceVo, MyResourceVo.class);
            myResourceVo.setTitle(resourceVo.getResourceName());
            myResourceVo.setChildren(new ArrayList<>());
            loadChildrenMenu(myResourceVo, systemResourceVos);
            if (myResourceVo.getOrder() == null) {
                myResourceVo.setOrder(0);
            }
            resourceList.add(myResourceVo);
        }
        //排序
        resourceList.sort(Comparator.comparingLong(MyResourceVo::getOrder));
        //递归查询资源code
        List<String> resourceCodes = new ArrayList<>();
        loadResourceCode(resourceCodes, resourceList);
        entity.setResourceCodes(resourceCodes);
        entity.setResourceVos(resourceList);
        RedisUtils.set(MAYAN_SYSTEM_MY_RESOURCE_CACHE + nowUser.getUserAccount(), entity);
        return entity;
    }

    private void loadResourceCode(List<String> resourceCodes, List<MyResourceVo> resourceVos) {
        for (MyResourceVo resourceVo : resourceVos) {
            String resourceCode = resourceVo.getResourceCode();
            resourceCodes.add(resourceCode);
            if (!ObjectUtils.isEmpty(resourceVo.getChildren())) {
                loadResourceCode(resourceCodes, resourceVo.getChildren());
            }
        }
    }

    private void loadChildrenMenu(MyResourceVo myResourceVo, List<MayanPlatSystemResourceVo> systemResourceVos) {
        for (MayanPlatSystemResourceVo resourceVo : systemResourceVos) {
            String resourceId = myResourceVo.getResourceId();
            if (resourceVo.getParentResourceId() != null && resourceVo.getParentResourceId().equals(resourceId)) {
                MyResourceVo child = BeanCopyUtils.copyObj(resourceVo, MyResourceVo.class);
                if (child != null && child.getOrder() == null) {
                    child.setOrder(0);
                }
                child.setTitle(myResourceVo.getTitle()+" > "+resourceVo.getResourceName());
                myResourceVo.getChildren().add(child);
                loadChildrenMenu(child, systemResourceVos);
                if (!ObjectUtils.isEmpty(myResourceVo.getChildren())) {
                    myResourceVo.getChildren().sort(Comparator.comparingLong(MyResourceVo::getOrder));
                }
            }
        }
    }


    /**
     * 递归查询父资源
     * @param resourceVos
     */
    private void loadParentResource(List<MayanPlatSystemResourceVo> resourceVos) {
        List<MayanPlatSystemResourceVo> parentResourceList = new ArrayList<>();
        for (MayanPlatSystemResourceVo resourceVo : resourceVos) {
            String parentResourceId = resourceVo.getParentResourceId();
            if (parentResourceId != null
                    && parentResourceList.stream().noneMatch(r -> r.getResourceId().equals(parentResourceId))
                    && resourceVos.stream().noneMatch(r -> r.getResourceId().equals(parentResourceId))) {
                LambdaQueryWrapper<MayanPlatSystemResource> wrapper = mayanPlatSystemResourceService.wrapper();
                wrapper.eq(MayanPlatSystemResource::getResourceId, parentResourceId);
                MayanPlatSystemResourceVo parentResource = mayanPlatSystemResourceService.queryOne(wrapper);
                if (!ObjectUtils.isEmpty(parentResource)) {
                    parentResourceList.add(parentResource);
                    loadParentResource(parentResourceList);
                }
            }
        }
        resourceVos.addAll(parentResourceList);
    }

    @Override
    public MayanPlatUserVo getById(String id) {
        MayanPlatUserVo platUser = mayanPlatUserService.queryOne(id);
        if (!ObjectUtils.isEmpty(platUser) && !ObjectUtils.isEmpty(platUser.getSuperAccount())) {
            MayanPlatUserVo superUser = mayanPlatUserService.queryByUserAccount(platUser.getSuperAccount());
            if (!ObjectUtils.isEmpty(superUser)) {
                platUser.setSuperUsername(superUser.getUsername());
            }
        }
        return platUser;
    }

    @Override
    public synchronized void add(MayanPlatUserVo userVo) {
        String superAccount = userVo.getSuperAccount();
        if (!ObjectUtils.isEmpty(superAccount)) {
            MayanPlatUserVo superUser = mayanPlatUserService.queryByUserAccount(superAccount);
            AssertUtils.isTrue(!ObjectUtils.isEmpty(superUser), "上级账号错误或不存在");
        }
        String username = userVo.getUsername();
        AssertUtils.isTrue(!ObjectUtils.isEmpty(username), "请输入姓名");
        //设置用户账号
        if (ObjectUtils.isEmpty(userVo.getUserAccount())) {
            String userAccount = pinyinByUserName(username);
            userVo.setUserAccount(userAccount);
        } else {
            MayanPlatUserVo oldUserVo = mayanPlatUserService.queryByUserAccount(userVo.getUserAccount());
            AssertUtils.isTrue(ObjectUtils.isEmpty(oldUserVo), "员工账号" + userVo.getUserAccount() + "已存在");
        }
        //员工编号
        String userId = userId();
        userVo.setUserId(userId);
        //设置初始密码
        String password = Md5Utils.MD5Encoder("123456");
        userVo.setPassword(password);
        userVo.setStatus(ON_JOB);
        mayanPlatUserService.saveVo(userVo);
    }

    private String userId() {
        String s = mayanPlatUserMapper.queryUserId();
        long l = Long.parseLong(s);
        return String.valueOf(l + 1);
    }

    private String pinyinByUserName(String username) {
        String pinyin = PinyinUtils.pinyin(username);
        MayanPlatUserVo userVo = mayanPlatUserService.queryByUserAccount(pinyin);
        String firstPinyin = pinyin;
        int index = 1;
        while (!ObjectUtils.isEmpty(userVo)) {
            pinyin = firstPinyin + index;
            userVo = mayanPlatUserService.queryByUserAccount(pinyin);
            index++;
        }
        return pinyin;
    }

    @Override
    public void update(MayanPlatUserVo userVo) {
        String superAccount = userVo.getSuperAccount();
        if (!ObjectUtils.isEmpty(superAccount)) {
            MayanPlatUserVo superUser = mayanPlatUserService.queryByUserAccount(superAccount);
            AssertUtils.isTrue(!ObjectUtils.isEmpty(superUser), "上级账号错误或不存在");
        }
        mayanPlatUserService.saveVo(userVo);
    }

    @Override
    public void updateNowUser(MayanPlatUserVo userVo) {
        update(userVo);
        UserInfo nowUser = UserHelper.getNowUser();
        MayanPlatUserVo user = nowUser.getUser();
        user.setUsername(userVo.getUsername());
        user.setNickName(userVo.getNickName());
        user.setEmail(userVo.getEmail());
        user.setImg(userVo.getImg());
        UserHelper.refreshUserInfo(nowUser);
    }


    @Autowired
    private MayanPlatUserMapper mayanPlatUserMapper;

    @Override
    public PageVo<MayanPlatUserVo> userList(UserPageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<MayanPlatUserVo> list = mayanPlatUserMapper.queryList(pageRequest);
        return PageVo.page(list);
    }

    @Override
    public List<MayanPlatUserVo> queryUserList(String key) {
        if (ObjectUtils.isEmpty(key)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<MayanPlatUser> wrapper = mayanPlatUserService.wrapper();
        wrapper.and(wr -> wr.like(MayanPlatUser::getUserAccount, key)
                .or().like(MayanPlatUser::getUsername, key)
                .or().like(MayanPlatUser::getNickName, key));
        return mayanPlatUserService.queryVoList(wrapper);
    }

    @Override
    public void resetPassword(String account) {
        MayanPlatUserVo userVo = mayanPlatUserService.queryByUserAccount(account);
        if (!ObjectUtils.isEmpty(userVo)) {
            String newPassword = mayanPlatParameterService.getString(USER_RESET_PASSWORD_VALUE);
            if (ObjectUtils.isEmpty(newPassword)) {
                newPassword = "123456";
            }
            userVo.setPassword(Md5Utils.MD5Encoder(newPassword));
            userVo.setChangePassTime(new Date());
            mayanPlatUserService.saveVo(userVo);
        }
    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {
        Assert.isTrue(!ObjectUtils.isEmpty(oldPassword), "原密码不可为空");
        Assert.isTrue(!ObjectUtils.isEmpty(newPassword), "新密码不可为空");
        UserInfo nowUser = UserHelper.getNowUser();
        MayanPlatUserVo userVo = userService.getById(nowUser.getUser().getId());
        if (!Md5Utils.MD5Encoder(oldPassword).equals(userVo.getPassword())) {
            throw new BusinessException("原密码错误");
        }
        //校验新密码必须为6位以上，且只能是数字和字母组合
        AssertUtils.isTrue(newPassword.length() >= 6, "新密码长度不能小于6位");
        AssertUtils.isTrue(!oldPassword.equals(newPassword), "原密码和新密码不能一致");
//        AssertUtils.isTrue(newPassword.matches("[a-zA-Z0-9]+"), "新密码只能是数字和字母组合");

        userVo.setPassword(Md5Utils.MD5Encoder(newPassword));
        userVo.setChangePassTime(new Date());
        mayanPlatUserService.saveVo(userVo);
    }

    @Override
    public void disabled(String userAccount) {
        MayanPlatUserVo userVo = mayanPlatUserService.queryByUserAccount(userAccount);
        if (!ObjectUtils.isEmpty(userVo)) {
            String status = userVo.getStatus();
            if (OFF_JOB.equals(status)) {
                userVo.setStatus(ON_JOB);
            } else {
                userVo.setStatus(OFF_JOB);
            }
            mayanPlatUserService.saveVo(userVo);
        }
    }
}
